<template>
  <div>
    <el-row class="app-toptool" type="flex">
      <el-col :span="16">
        <Search
          size="small"
          :search-visible="searchVisible"
          :search-data.sync="searchData"
          :search-form="searchForm"
          @refesh_list="getList"
        />
      </el-col>
      <el-col :span="8">
        <div class="btn-group" style="margin-bottom: 11px; float: right">
          <div>
            <el-button
              v-if="checkPermission('/stock/update')"
              type="primary"
              size="small"
              icon="el-icon-plus"
              @click="add()"
              >添加</el-button
            >
            <el-button
              v-if="checkPermission('/stock/delete')"
              type="primary"
              :disabled="multiple"
              size="small"
              icon="el-icon-delete"
              @click="del(ids)"
              >删除</el-button
            >

            <!-- <el-button
              v-if="checkPermission('/store/dumpdata')"
              type="primary"
              size="small"
              icon="el-icon-download"
              @click="dumpdata(ids)"
              >导出</el-button
            > -->
            <el-button
              v-if="checkPermission('/store/dumpdata')"
              type="primary"
              size="small"
              icon="el-icon-download"
              @click="exportData"
              >导出明细</el-button
            >
          </div>
        </div>
      </el-col>
    </el-row>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :row-class-name="rowClass"
      :tree-props="{ children: 'children' }"
      :default-expand-all="expand"
      row-key="id"
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      :border="false"
      :stripe="true"
      class="eltable"
      :data="list"
      style="width: 100%"
      @selection-change="selection"
    >
      <el-table-column align="center" type="selection" width="42" />
      <el-table-column
        align="center"
        type=""
        property="id"
        label="编号"
        show-overflow-tooltip
        width="70"
      />
      <el-table-column
        align="left"
        property="name"
        label="名称"
        show-overflow-tooltip
        width=""
      />
      <el-table-column
        align="left"
        property="specification"
        label="规格"
        show-overflow-tooltip
        width=""
      />
      <el-table-column
        align="left"
        property="stock"
        label="库存"
        show-overflow-tooltip
        width=""
      />
      <el-table-column
        align="left"
        property="quantity_unit"
        label="单位"
        show-overflow-tooltip
        width=""
      />
      <el-table-column
        align="left"
        property="price"
        label="价格"
        show-overflow-tooltip
        width=""
      />
      <el-table-column
        align="left"
        property="sale_count"
        label="已领用"
        show-overflow-tooltip
        width=""
      >
        <template slot-scope="scope">
          <div style="color: dodgerblue" @click="getDetail(scope.row)">
            {{ scope.row.sale_count }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="left"
        property="create_time"
        label="时间"
        show-overflow-tooltip
        width=""
      />
      <el-table-column
        align="center"
        property="status"
        label="状态"
        show-overflow-tooltip
        width=""
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="listUpdate(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.id">
            <el-button
              v-if="checkPermission('/stock/detail')"
              size="mini"
              type="primary"
              @click="update(scope.row)"
              ><i class="el-icon-edit" />修改</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div style="height: 60px" />
    <!--添加/修改-->
    <Update
      :info="updateInfo"
      :opentype="opentype"
      :show.sync="dialog.updateDialogStatus"
      size="small"
      @refesh_list="getList"
    />
    <ReceiveDetail
      :id="currentId"
      @colse="showDetail = false"
      v-if="showDetail"
    />
  </div>
</template>

<script>
import Search from "@/components/common/Search";
import Update from "@/views/material/update.vue";
import waves from "@/directive/waves"; // waves directive
import ReceiveDetail from "@/views/material/receiveDetail.vue";
import { confirm, param2Obj } from "@/utils/common";

import XLSX from "xlsx";
export default {
  name: "Servicecategory",
  directives: { waves },
  components: {
    Search,
    Update,
    ReceiveDetail,
  },
  data() {
    return {
      dialog: {
        updateDialogStatus: false,
      },
      ids: [],
      single: true,
      multiple: true,
      list: [],
      opentype: "add",
      updateInfo: {},
      loading: false,
      searchVisible: true,
      searchForm: [],
      page_data: {
        limit: 20,
        page: 1,
        total: 20,
      },
      searchData: {},
      expand: true,
      dumpshow: false,
      exceldata: [],
      dumppage: 1,
      percentage: 0,
      ws: {},
      filename: "",
      showDetail: false,
      currentId: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      const param = {
        path: this.$route.path,
        limit: this.page_data.limit,
        page: this.page_data.page,
      };
      Object.assign(param, this.searchData);
      Object.assign(param, param2Obj(this.$route.fullPath));
      this.loading = true;
      this.$api.post("/stock/index", param).then((res) => {
        this.list = res.data.data;
        this.page_data.total = res.data.total;
        this.loading = false;

        this.searchForm = [
          {
            type: "Input",
            label: "关键词",
            prop: "keyword",
            width: "230px",
          },
        ];
      });
    },
    // listUpdate(row, field) {
    //   if (row.id) {
    //     this.$api.post('/Category/listUpdate', {
    //       id: row.id,
    //       [field]: row[field]
    //     }).then(res => {
    //       this.$message({
    //         message: '操作成功',
    //         type: 'success'
    //       })
    //     })
    //   }
    // },
    add() {
      this.opentype = "add";
      this.dialog.updateDialogStatus = true;
    },
    update(row) {
      this.opentype = "update";
      const id = row.id ? row.id : this.ids.join(",");
      this.$api
        .post("/stock/detail", {
          id: id,
        })
        .then((res) => {
          this.dialog.updateDialogStatus = true;
          this.updateInfo = res.data;
        });
    },
    del(row) {
      confirm({
        content: "确定要操作吗",
      })
        .then(() => {
          const ids = row.id ? row.id : this.ids.join(",");
          this.$api
            .post("/stock/delete", {
              id: ids,
            })
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getList();
            });
        })
        .catch(() => {});
    },
    selection(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    rowClass({ row, rowIndex }) {
      for (let i = 0; i < this.ids.length; i++) {
        if (row.id === this.ids[i]) {
          return "rowLight";
        }
      }
    },
    toggleRowExpansion() {
      this.expand = !this.expand;
      this.list.forEach((item) => {
        this.$refs.multipleTable.toggleRowExpansion(item, this.expand);
      });
    },
    dumpdata() {
      confirm({
        content: "确定要操作吗",
      })
        .then(() => {
          this.dumpshow = true;
          this.confirmdumpdata();
        })
        .catch(() => {});
    },
    confirmdumpdata() {
      let param = {
        path: this.$route.path,
        page: this.dumppage,
      };
      if (this.searchData) param = Object.assign(param, this.searchData);
      this.$api.post("/stock/dumpdata", param).then((res) => {
        console.log(res, 666);
        if (res.data.length > 0) {
          if (this.dumppage == 1) {
            this.exceldata.push(res.header);
          }
          res.data.forEach((item) => {
            this.exceldata.push(Object.values(item));
          });
          this.percentage = res.percentage;
          this.filename = res.filename;
          this.ws = XLSX.utils.aoa_to_sheet(this.exceldata);
          this.dumppage = this.dumppage + 1;
          this.confirmdumpdata();
        } else {
          const wb = XLSX.utils.book_new();
          XLSX.utils.book_append_sheet(wb, this.ws, "资料");
          XLSX.writeFile(wb, this.filename);
          this.dumpshow = false;
        }
      });
    },
    closedialog() {
      this.dumpshow = false;
    },
    getDetail(item) {
      this.currentId = item.id;
      this.showDetail = true;
    },
    exportData() {
      window.open(
        "https://jiazheng.shwlnft.site/addons/xm_mallv3/samos/public/index.php?s=/admin/Stockorder/dumplog",
        "_blank"
      );
      // window.location.href="https://daili.f0710.com/addons/xm_mallv3/samos/public/index.php?s=/admin/Stockorder/dumplog"
    },
  },
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}

.table_list_pic {
  width: 60px;
  height: 60px;
  vertical-align: middle;
}
</style>
